import request from '@/axios'
import type { RoleQueryCriteria, RoleGridView, RoleForm, RoleAuthorization } from './types';

const pathPrefix = '/api/role'

export const query = (data: PaginationRequest<RoleQueryCriteria>): Promise<PaginationResponse<RoleGridView>> => {
    return request.post<PaginationResponse<RoleGridView>>({
        url: `${pathPrefix}/query`,
        data
    })

}

export const add = (data: RoleForm): Promise<ApiResult<string>> => {
    return request.post<ApiResult<string>>({
        url: `${pathPrefix}`,
        data
    })
}

export const edit = (id: string, data: RoleForm): Promise<ApiResult<string>> => {
    return request.put<ApiResult<string>>({
        url: `${pathPrefix}/${id}`,
        data
    })
}

export const getForm = (id: string): Promise<RoleForm> => { 
    return request.get<RoleForm>({
        url: `${pathPrefix}/${id}`
    })
}

export const remove = (id: string): Promise<ApiResult<string>> => {
    return request.delete({
        url: `${pathPrefix}/${id}`
    })
}

export const getAuthorizedResources = (id: string): Promise<string[]> => {
    return request.get<string[]>({
        url: `${pathPrefix}/${id}/authorized`
    })
}

export const authorize = (data: RoleAuthorization) => {
    return request.put({
        url: `${pathPrefix}/authorize`,
        data
    })
}

export const unauthorize = (data: RoleAuthorization) => {
    return request.delete({
        url: `${pathPrefix}/authorize`,
        data
    })
}